// 用于创建vuex核心store

// 引入vue
import Vue from "vue";
import Vuex from 'vuex';
import countOptions from "./count.js";
import axios from 'axios'
import { nanoid } from "nanoid";
Vue.use(Vuex);

// 人员功能相关的配置
const personOptions = {
    namespaced: true,
    actions: {
        addPersonWang(context, value) {
            if (value.name.indexOf('王') === 0) {
                context.commit('ADD_PERSON', value);
            }
        },
        addPersonServer(context) {
            axios.get('https://api.oioweb.cn/api/SoulWords')
                .then(
                    response => {
                        context.commit('ADD_PERSON', { id: nanoid(), name: response.data.result.content });
                    },
                    error => {
                        alert(error.msg);
                    }
                )
        },
    },
    mutations: {
        ADD_PERSON(state, value) {
            state.personList.unshift(value);
        },
    },
    state: {
        personList: [
            { id: '001', name: '张三' },
        ]
    },
    getters: {
        firstPersonName(state) {
            return state.personList[0].name;
        }
    }
};

//创建并导出store
export default new Vuex.Store({
    modules: {
        count: countOptions,
        person: personOptions,
    }
});